package demo;

class Animal {
	protected String name;// 动物名
	protected int x;// 坐标
	protected int y;
	protected int range;// 步数范围（）

	public void show() {
		System.out.println("这是一只" + name);
		System.out.println("它的起始坐标是在x:" + this.x + "y:" + this.y);
	}

	public Animal(String name, int x, int y, int range) {
		this.name = name;
		this.x = x;
		this.y = y;
		this.range = range;
	}

	public String eat() {
		String str = "x:" + this.x + "y:" + this.y;
		return str;
	}

	public void move() {

		int max = 30, min = 1;
		int ma = 40, mi = 60;
		int ran2 = (int) (Math.random() * (ma - mi) + mi);
		int ran1 = (int) (Math.random() * (max - min) + min);

		if (ran1 >= 0 && ran1 <= 10 && ran2 >= 40 && ran2 <= 50) {
			this.x = this.x + this.range;
			System.out.println("\n" + this.name + "现在的坐标是在x:" + this.x + "\ty:" + this.y);
		} else {
			System.out.println("\n" + this.name + "现在的坐标是在x:" + this.x + "\ty:" + this.y);
		}
		if (ran1 >= 20 && ran1 <= 30 && ran2 >= 50 && ran2 <= 60) {
			this.y = this.y + this.range;
			System.out.println("\n" + this.name + "现在的坐标是在x:" + this.x + "\ty:" + this.y);
		} else {
			System.out.println("\n" + this.name + "现在的坐标是在x:" + this.x + "\ty:" + this.y);
		}
	}
}

class Tiger extends Animal {

	public Tiger(String name, int x, int y, int range) {
		// TODO Auto-generated constructor stub
		super(name, x, y, range);
	}
}

class Wolf extends Animal {
	public Wolf(String name, int x, int y, int range) {
		// TODO Auto-generated constructor stub
		super(name, x, y, range);
	}
}

class Sheep extends Animal {
	public Sheep(String name, int x, int y, int range) {
		// TODO Auto-generated constructor stub
		super(name, x, y, range);
	}
}

public class demo {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println("在面积为 500 X 500 立方米的矩形草地上。（老虎吃狼、羊，狼吃羊）");
		Animal a[] = { new Sheep("羊", 3, 2, 1), new Wolf("狼", 2, 1, 1), new Tiger("老虎", 1, 1, 1) };
		a[0].show();
		a[1].show();
		a[2].show();
		Boolean o1 = true, o2 = true;
		while (true) {
			if (o1 == true && a[0].eat().equals(a[1].eat())) {
				System.out.println("\n羊坐标: x=" + a[0].x + "  y=" + a[0].y + "  狼坐标:x=" + a[1].x + "  y=" + a[1].y);
				System.out.println("!!!坐标重叠  羊被狼吃了!!!");
				o1 = false;
			}
			if (o2 == true && a[1].eat().equals(a[2].eat())) {
				System.out.println("\n狼坐标: x=" + a[1].x + "  y=" + a[1].y + "  老虎坐标:x=" + a[2].x + "  y=" + a[2].y);
				System.out.println("!!!坐标重叠  狼被老虎吃了!!!");
				o2 = false;
			}
			if (o1 == true && a[0].eat().equals(a[2].eat())) {
				System.out.println("\n羊坐标: x=" + a[0].x + "  y=" + a[0].y + "  老虎坐标:x=" + a[2].x + "  y=" + a[2].y);
				System.out.println("!!!坐标重叠  羊被老虎吃了!!!");
				o1 = false;
			}
			if (o1 == true && o2 == true) {// 狼，羊，老虎
				int ran2 = (int) (Math.random() * (500 - 1) + 1);
				switch (ran2 / 3) {
				case 0:
					a[0].move();
					a[2].move();
					a[1].move();
					break;
				case 1:
					a[2].move();
					a[1].move();
					a[0].move();
					break;
				case 2:
					a[1].move();
					a[0].move();
					a[2].move();
					break;
				}
			} else if (o1 == true && o2 == false) {// 羊，老虎
				int ran2 = (int) (Math.random() * (500 - 1) + 1);
				switch (ran2 / 2) {
				case 0:
					a[0].move();
					a[2].move();
					break;
				case 1:
					a[2].move();
					a[0].move();
					break;
				}
			} else if (o1 == false && o2 == true) {// 狼，老虎
				int ran2 = (int) (Math.random() * (500 - 1) + 1);
				switch (ran2 / 2) {
				case 0:
					a[1].move();
					a[2].move();
					break;
				case 1:
					a[2].move();
					a[1].move();
					break;
				}
			} else {// 老虎
				a[2].move();
			}

			if (a[0].x >= 100 && a[0].y >= 100) {
				System.out.println("羊到达终点了!!!  坐标：x=" + a[0].x + "  y=" + a[0].y);
				break;
			}
			if (a[1].x >= 95 && a[1].y >= 95) {
				System.out.println("狼到达终点了!!!  坐标：x=" + a[1].x + "  y=" + a[1].y);
				break;
			}
			if (a[2].x >= 95 && a[2].y >= 95) {
				System.out.println("老虎到达终点了!!!  坐标：x=" + a[2].x + "  y=" + a[2].y);
				break;
			}
		}
	}
}
